import type { Dict, DictionaryOption } from '#/Dictionary.d.ts';
/**
 * @deprecated 已弃用
 */
export const formatIdsDict = async ({ vueObj, cellValue, dictName }: {
	vueObj: any,
	cellValue: any,
	dictName: any
}): Promise<any> => {
	if (cellValue) {
		return cellValue;
	}
	const dict = vueObj.store['state']['dictionary']['dictionary'][dictName];
	let val;
	if (dict) {
		for (const value of dict) {
			if (cellValue.toString() === value['DICT_CODE']) {
				val = value['DICT_NAME'];
				break;
			}
		}
	}
	// 如果上面依旧无法对应上字典,那么就将原有数据返回
	if (!val) {
		val = cellValue;
	}
	return val;
};
/**
 * @deprecated 已弃用
 */
export const formatIdsDictSelect = async ({ vueObj, dictName }: { vueObj: any, dictName: any }): Promise<any> => {
	const dict = vueObj.store['state']['dictionary']['dictionary'][dictName];
	const arr = [];
	if (dict) {
		for (const value of dict) {
			arr.push({ label : value['DICT_NAME'], value : value['DICT_CODE'] });
		}
	}
	return arr;
};
/**
 * @deprecated 已弃用
 */
export const formatQueryIdsDict = async ({ vueObj, cellValue, dictName }: {
	vueObj: any,
	cellValue: any,
	dictName: any
}): Promise<any> => {
	if (cellValue) {
		return cellValue;
	}
	const dictionary = vueObj.store['state']['dictionary']['dictionary'][dictName];
	let val;
	if (dictionary) {
		for (const value of dictionary) {
			if (cellValue.toString() === value['KEY']) {
				val = value['VALUE'];
				break;
			}
		}
	}
	// 如果上面依旧无法对应上字典,那么就将原有数据返回
	if (!val) {
		val = cellValue;
	}
	return val;
};
/**
 * @deprecated 已弃用
 */
export const formatQueryIdsDictByList = async (cellValue: any, list: any): Promise<any> => {
	if (cellValue) {
		return cellValue;
	}
	const dictionary = list;
	let val;
	if (dictionary) {
		for (const value of dictionary) {
			if (cellValue.toString() === value['KEY']) {
				val = value['VALUE'];
				break;
			}
		}
	}
	// 如果上面依旧无法对应上字典,那么就将原有数据返回
	if (!val) {
		val = cellValue;
	}
	return val;
};
/**
 * 用于将非标准字典类型格式化成标准naiveUI的select支持的类型
 * @param dict Array<any>
 * @param labelString 用于展示的非标准字典字段
 * @param valueString 用于数据的非标准字典字段
 */
export const formatDictSelectBy = async (
	dict: Array<any>,
	labelString: string,
	valueString: string
): Promise<string | any> => {
	const arr: any[] = [];
	if (dict && Array.isArray(dict) && dict.length) {
		dict.forEach((item: any): void => {
			arr.push({ label : item[labelString], value : item[valueString] });
		});
		return arr;
	} else {
		return `字典[${dict}]不存在,你确定你加载过了?`;
	}
};
/**
 * 该方法通过传入(字典名称,字典对象,值)来返回字典值所对应的值
 * @param dictName string 字典名称
 * @param dict Dict 字典对象
 * @param value string | number | boolean 值
 */
export const showDictValueByKey1 = (
	dictName: string,
	dict: Dict,
	value: string | number | boolean
): string | any => {
	if (!dict) {
		return '[dict]参数为null/undefined';
	}
	if (dict[dictName] && Array.isArray(dict[dictName]) && dict[dictName].length) {
		// 寻找匹配值的结果
		const result: DictionaryOption | undefined =
				  dict[dictName].find((item: any): boolean => item.value === value);
		if (result) {
			// 如果匹配上,就返回label
			return result.label;
		} else {
			// 如果匹配不上,就返回value
			return value;
		}
	} else {
		return `字典[${dictName}]不存在,你确定你加载过了?`;
	}
};
/**
 * 该方法通过传入(字典名称,字典对象,值)
 * <br/>
 * 来返回字典值所对应的值
 * <br/>
 * 与showDictValueByKey1的区别是,
 * <br/>
 * 该方法会返回[value]label,
 * <br/>
 * 而showDictValueByKey1会返回value
 * @param dictName string 字典名称
 * @param dict Dict 字典对象
 * @param value string | number | boolean 值
 */
export const showDictValueByKey2 = (
	dictName: string,
	dict: Dict,
	value: string | number | boolean
): string | any => {
	if (!dict) {
		return '[dict]参数为null/undefined';
	}
	if (dict[dictName] && Array.isArray(dict[dictName]) && dict[dictName].length) {
		// 寻找匹配值的结果
		const result: DictionaryOption | undefined =
				  dict[dictName].find((item: any): boolean => item.value === value);
		if (result) {
			// 如果匹配上,就返回label
			return `[${result.value}]${result.label}`;
		} else {
			// 如果匹配不上,就返回value
			return value;
		}
	} else {
		return `字典[${dictName}]不存在,你确定你加载过了?`;
	}
};